import { HomeModel } from '../../models/home.js'

const ModelHome = new HomeModel()
Component({
  properties: {
    visible: Boolean,
    serviceInfo: Object
  },
  data: {
    type: '2', // 1.预告查询 2.活动中查询(默认传2)
    specialName: '',
    activityNum: 0,
    willActivityNum: 0,
    brandText: '',
    ready: false
  },
  attached() {
    this.fectchData()
  },
  /**
   * 组件的方法列表
   */
  methods: {
    fectchData() {
      const params = {
        type: this.data.type,
        specialName: this.data.specialName
      }
      this.setData({
        ready: false
      })
      ModelHome.searchBrands(params)
        .then(res => {
          this.setData({
            brandList: []
          })
          // 正在进行中
          if (this.data.type === '2') {
            this.setData({
              brandList: res.data.specialList.activityList,
              ready: true
            })
            // 预告
          } else {
            this.setData({
              brandList: res.data.specialList.willActivityList,
              ready: true
            })
          }
          this.setData({
            type: this.data.type,
            activityNum: res.data.activityNum,
            willActivityNum: res.data.willActivityNum
          })
        })
        .catch(err => {})
    },
    // 切换tab
    brandTab(e) {
      this.setData({
        type: e.currentTarget.dataset.type,
        brandList: []
      })
      this.fectchData()
    },
    // 关闭closeBrandPopup
    closeBrandPopup() {
      this.setData({
        visible: false
      })
    },
    inputSearchBrands(e) {
      //输入内容
      this.setData({
        brandText: e.detail.value
      })
    },
    searchBrands() {
      this.data.specialName = this.data.brandText
      this.fectchData()
    },
    // 清理
    clearSearchText() {
      if (this.data.brandText == '') {
        return
      }
      this.setData({
        brandText: ''
      })
      this.searchBrands()
    },
    // 去专场
    goActivity(event) {
      const item = event.currentTarget.dataset.item

      if (this.data.type === '2') {
        this.setData({
          visible: false
        })
        wx.navigateTo({
          url: `/pages/activity/index?scode=${item.specialCode}`
        })
      } else {
        //去专场预览
        // 如果预告已经开启
        if (item.specialOpen) {
          wx.navigateTo({
            url: `/pages/trailer/index?scode=${item.specialCode}`
          })
          this.setData({
            visible: false
          })
        } else {
          wx.showToast({
            title: '暂时无法预览该专场',
            icon: 'none'
          })
        }
      }
    }
  }
})
